package exam.springsecurity.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import exam.config.Utils;
import exam.springsecurity.domain.User;
import exam.springsecurity.mapper.UserMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/4/3 21:14
 * @since JDK 1.8
 */
@Slf4j
@RestController
@Api(tags = {"用户登入模块"})
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @PutMapping()
    public JSONObject addUserController(@ApiParam("用户账号")@RequestParam("username")String userName,
                                        @ApiParam("用户密码")@RequestParam("password")String passWord,
                                        @ApiParam("用户昵称")@RequestParam("realname")String realName,
                                        @ApiParam("用户手机号")@RequestParam("phone")String phone){
        JSONObject jsonObject=new JSONObject();
        if(StringUtils.isEmpty(userName)||StringUtils.isEmpty(passWord)||StringUtils.isEmpty(phone)
        ||StringUtils.isEmpty(realName)){
            jsonObject.put("code",1);
            jsonObject.put("msg","系统维护中");
            return jsonObject;
        }

        String nowDate= Utils.getNowDate();
        User user=new User(null,userName,passWord,realName,phone,0,nowDate);
        userMapper.insert(user);

        jsonObject.put("code",0);
        jsonObject.put("msg","success");
        return jsonObject;
    }

    @GetMapping()
    public JSONObject getUserListPageController(@ApiParam("页码")@RequestParam("page") Integer page,
                                                @ApiParam("条数")@RequestParam("pageSize") Integer pageSize,
                                                @ApiParam("账号")@RequestParam("userName") String userName,
                                                @ApiParam("用户昵称")@RequestParam("realName") String realName){
        JSONObject jsonObject=new JSONObject();
        LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper();
        if(!StringUtils.isEmpty(userName)){
            lambdaQueryWrapper.like(User::getUsername,userName);
        }
        if(!StringUtils.isEmpty(realName)){
            lambdaQueryWrapper.like(User::getRealName,realName);
        }
        IPage<User> userIPage=userMapper.selectPage(new Page(page,pageSize),lambdaQueryWrapper);
        jsonObject.put("code",0);
        jsonObject.put("msg","success");
        jsonObject.put("data",userIPage);

        return jsonObject;
    }

    @PostMapping("/login")
    public JSONObject loginController(@ApiParam("账号")@RequestParam("username")String userName,
                                      @ApiParam("密码")@RequestParam("password")String password){
      log.info("有人来访问我了");
      log.info("账号:"+userName);
      log.info("密码"+password);

      JSONObject jsonObject=new JSONObject();
      jsonObject.put("code",0);
      return jsonObject;

    }
}
